package com.zzyl.serve.service.impl;

import java.util.List;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zzyl.common.core.redis.RedisCache;
import com.zzyl.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import com.zzyl.serve.mapper.NursingPlanMapper;
import com.zzyl.serve.domain.NursingPlan;
import com.zzyl.serve.service.INursingPlanService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import java.util.Arrays;

/**
 * 护理计划Service业务层处理
 * 
 * @author ruoyi
 * &#064;date  2024-10-04
 */
@Service
public class NursingPlanServiceImpl extends ServiceImpl<NursingPlanMapper, NursingPlan> implements INursingPlanService
{
    @Autowired
    private NursingPlanMapper nursingPlanMapper;

    @Autowired
//    RedisTemplate<Object, Object> redisTemplate;
    // 这里用若依自带的缓存类
    RedisCache cache;
    private static final String CACHE_NURSING_PLAN = "nursingPlan:all";

    /**
     * 查询护理计划
     * 
     * @param id 护理计划主键
     * @return 护理计划
     */
    @Override
    public NursingPlan selectNursingPlanById(Long id)
    {
        return getById(id);
    }

    /**
     * 查询护理计划列表
     * 
     * @param nursingPlan 护理计划
     * @return 护理计划
     */
    @Override
    public List<NursingPlan> selectNursingPlanList(NursingPlan nursingPlan)
    {
        return nursingPlanMapper.selectNursingPlanList(nursingPlan);
    }

    /**
     * 新增护理计划
     * 
     * @param nursingPlan 护理计划
     * @return 结果
     */
    @Override
    public int insertNursingPlan(NursingPlan nursingPlan)
    {
        deleteCache();
        return save(nursingPlan) ? 1 : 0;
    }

    /**
     * 修改护理计划
     * 
     * @param nursingPlan 护理计划
     * @return 结果
     */
    @Override
    public int updateNursingPlan(NursingPlan nursingPlan)
    {
        deleteCache();
        return updateById(nursingPlan) ? 1 : 0;
    }

    /**
     * 批量删除护理计划
     * 
     * @param ids 需要删除的护理计划主键
     * @return 结果
     */
    @Override
    public int deleteNursingPlanByIds(Long[] ids)
    {
        deleteCache();
        return removeByIds(Arrays.asList(ids)) ? 1 : 0;
    }

    /**
     * 删除护理计划信息
     * 
     * @param id 护理计划主键
     * @return 结果
     */
    @Override
    public int deleteNursingPlanById(Long id)
    {
        deleteCache();
        return removeById(id) ? 1 : 0;
    }

    /**
     * 查询所有启用的护理等级
     * @return
     */
    @Override
    public List<NursingPlan> listAll() {
        // 1.先查询缓存数据所有护理等级
//        List<NursingPlan> list = (List<NursingPlan>) redisTemplate.opsForValue().get(CACHE_NURSING_PLAN);
        List<NursingPlan> nursingPanList = cache.getCacheObject(CACHE_NURSING_PLAN);
        // 2.判断查询的缓存数据是否有效，如果有效直接返回
        if(CollectionUtil.isNotEmpty(nursingPanList)){
            return nursingPanList;
        }
        // 3.如果缓存数据无效，执行数据库操作查询所有启用的等级
        LambdaQueryWrapper<NursingPlan> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(NursingPlan::getStatus, 1);
        nursingPanList = nursingPlanMapper.selectList(queryWrapper);
        // 4.将数据库查到的所有等级写入缓存
//        redisTemplate.opsForValue().set(CACHE_NURSING_PLAN, nursingPanList);
        cache.setCacheObject(CACHE_NURSING_PLAN,nursingPanList);
        // 5.返回数据
        return  nursingPanList;
    }

    private void deleteCache() {
        // 删除缓存
//        redisTemplate.delete(CACHE_NURSING_PLAN);
        cache.deleteObject(CACHE_NURSING_PLAN);
//        // 重新加载缓存
//        List<NursingPlan> nursingPlans = listAll();
//        redisTemplate.opsForValue().set(CACHE_KEY_PREFIX, nursingPlans);
    }
}
